package designPattern;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Home{
   void SaleHome();
}



class HomeUtil implements Home{
    public void SaleHome() {
        System.out.println("我开始卖家了");
    }
}


class JdkRunProxy implements InvocationHandler{

    private Object target ;

    public JdkRunProxy(Object target){
        this.target = target;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开启事务");
        Object object = method.invoke(target,args);
        System.out.println("结束事务");
        return object;
    }
}


/**jdk动态代理*/
public class JdkProxy {
    public static void main(String[] args) {
        HomeUtil homeUtil = new HomeUtil();
        JdkRunProxy jdkRunProxy = new JdkRunProxy(homeUtil);
        Home home = (Home) Proxy.newProxyInstance(homeUtil.getClass().getClassLoader(),homeUtil.getClass().getInterfaces(),jdkRunProxy);
        home.SaleHome();
    }
}
